//
//  CustomImage.m
//  MeZoneB
//
//  Created by d2space on 14-8-6.
//  Copyright (c) 2014年 d2space. All rights reserved.
//

#import "CustomImage.h"

@implementation CustomImage
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    @autoreleasepool
    {
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        
        UIGraphicsBeginImageContext(rect.size);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context,
                                       
                                       color.CGColor);
        
        CGContextFillRect(context, rect);
        
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return img;
    }
}
+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

+ (UIImage *)changeImageColor:(UIImage *)sourceImage
{
    NSInteger width = sourceImage.size.width;
    NSInteger height = sourceImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL)
    {
        return  nil;
    }
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage.CGImage);
//    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    UIImage *grayImage = CFBridgingRelease(CGBitmapContextCreateImage(context));
    CGContextRelease(context);
    return grayImage;
}

+ (UIImage *)imageWithOrigin:(UIImage *)image size:(CGSize)size {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width * 8, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextDrawImage(context,
                       CGRectMake(0, 0, size.width, size.height),
                       image.CGImage);
    CGImageRef shrunken = CGBitmapContextCreateImage(context);
    UIImage *final = [UIImage imageWithCGImage:shrunken];
    
    CGContextRelease(context);
    CGImageRelease(shrunken);
    
    return final;
}
+ (UIImage *)imageWithShrunkenOrigin:(UIImage *)image size:(CGRect)rect {
    UIImage * resultImage = image;
    UIGraphicsBeginImageContext(rect.size);
    [resultImage drawInRect:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
    UIGraphicsEndImageContext();
    return resultImage;
}
+ (UIImage *)compressImage:(UIImage *)originImage {
    return [self compressImage:originImage maxSize:1000*1024]; // Byte字节
}

+ (UIImage *)compressImage:(UIImage *)originImage maxSize:(NSUInteger)maxSize {
    NSData * data = UIImageJPEGRepresentation(originImage, 1.0);
    CGFloat dataKBytes = data.length/1000.0;
    CGFloat maxQuality = 0.9f;
    CGFloat lastData = dataKBytes;
    while (dataKBytes > maxSize && maxQuality > 0.01f) {
        maxQuality = maxQuality - 0.01f;
        data = UIImageJPEGRepresentation(originImage, maxQuality);
        dataKBytes = data.length / 1000.0;
        if (lastData == dataKBytes) {
            break;
        }else{
            lastData = dataKBytes;
        }
    }
    UIImage *targetImage = [[UIImage alloc] initWithData:data];
    return targetImage;
}

+ (NSData *)compress:(UIImage *)originImage maxSize:(NSUInteger)maxSize{
    NSData * data = UIImageJPEGRepresentation(originImage, 1.0);
    CGFloat dataKBytes = data.length/1000.0;
    CGFloat maxQuality = 0.9f;
    CGFloat lastData = dataKBytes;
    while (dataKBytes > maxSize && maxQuality > 0.01f) {
        maxQuality = maxQuality - 0.01f;
        data = UIImageJPEGRepresentation(originImage, maxQuality);
        dataKBytes = data.length / 1000.0;
        if (lastData == dataKBytes) {
            break;
        }else{
            lastData = dataKBytes;
        }
    }
    return data;
}
+ (UIImage *)compressImageQuality:(UIImage *)image toByte:(NSInteger)maxLength {
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    if (data.length < maxLength) return image;
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    UIImage *resultImage = [UIImage imageWithData:data];
    return resultImage;
}



+ (UIImage *)drawTopAndBottomArcWithSize:(CGSize)size andColor:(UIColor *)fillColor andRadius:(CGFloat)radius {
    double PI = 3.14159265358979323846;
    UIGraphicsBeginImageContextWithOptions(size, NO, 2);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 0);
    
    UIBezierPath * path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(0, radius)];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:PI endAngle:PI / 2 clockwise:YES];
    [path addLineToPoint:CGPointMake(radius, 0)];
    [path addLineToPoint:CGPointMake(size.width - radius, 0)];
    [path addArcWithCenter:CGPointMake(size.width - radius, radius) radius:radius startAngle:PI endAngle:PI / 2 clockwise:YES];
    [path addLineToPoint:CGPointMake(size.width, radius)];
    [path addLineToPoint:CGPointMake(size.width, size.height - radius)];
    [path addArcWithCenter:CGPointMake(size.width - radius, size.height - radius) radius:radius startAngle:PI endAngle:PI / 2 clockwise:YES];
    [path addLineToPoint:CGPointMake(size.width - radius, size.height)];
    [path addLineToPoint:CGPointMake(radius, size.height)];
    [path addArcWithCenter:CGPointMake(radius, size.height - radius) radius:radius startAngle:-PI endAngle:PI / 2 * 3 clockwise:YES];
    [path addLineToPoint:CGPointMake(0, size.height - radius)];
    
    [path closePath];
    
    [fillColor setFill];
    [path fill];
    
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newPic;
}

+ (UIImage *)drawLineWithSize:(CGSize)size andColor:(UIColor *)color;
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@end
